package com.lcq.demo.controller;

import com.google.common.base.Preconditions;
import com.lcq.demo.common.entity.PageResult;
import com.lcq.demo.common.entity.Result;
import com.lcq.demo.entity.dto.ProductDTO;
import com.lcq.demo.entity.dto.ProductTypeDTO;
import com.lcq.demo.service.ProductTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 240701005
 */
@Tag(name = "产品分类管理")
@RestController
@RequestMapping("/api/productType")
@Slf4j
//@CrossOrigin(origins = "*", allowedHeaders = "token")
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;

    /**
     * 根据Id查询产品分类
     */
    @Operation(summary = "根据Id查询产品分类")
    @GetMapping("/{id}")
    public Result<ProductTypeDTO> getById(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductTypeController.getById.id:{}", id);
            }
            Preconditions.checkNotNull(id, "产品分类Id不能为空！");
            return Result.ok(productTypeService.getById(id));
        } catch (Exception e) {
            log.error("ProductTypeController.getById.error:{}", e.getMessage(), e);
            return Result.fail("查询产品分类失败！");
        }
    }

    /**
     * 新增产品分类
     */
    @Operation(summary = "新增产品分类")
    @PostMapping
    public Result<Boolean> addProductType(@RequestBody ProductTypeDTO productTypeDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductTypeController.addProductType.productTypeDTO:{}", productTypeDTO);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(productTypeDTO.getTypeName()), "产品分类名不能为空！");
            return Result.ok(productTypeService.insert(productTypeDTO));
        } catch (Exception e) {
            log.error("ProductTypeController.addProductType.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 更新产品分类
     */
    @Operation(summary = "更新产品分类")
    @PutMapping
    public Result<Boolean> updateProductType(@RequestBody ProductTypeDTO productTypeDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductTypeController.updateProductType.ProductTypeDTO:{}", productTypeDTO);
            }
            Preconditions.checkNotNull(productTypeDTO.getId(), "产品分类Id不能为空！");
            return Result.ok(productTypeService.update(productTypeDTO));
        } catch (Exception e) {
            log.error("ProductTypeController.updateProductType.error:{}", e.getMessage(), e);
            return Result.fail("更新产品分类失败！");
        }
    }

    /**
     * 根据ID删除产品分类
     */
    @Operation(summary = "删除产品分类")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProductType(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductTypeController.deleteProductType.id:{}", id);
            }
            Preconditions.checkNotNull(id, "产品分类Id不能为空！");
            return Result.ok(productTypeService.deleteById(id));
        } catch (Exception e) {
            log.error("ProductTypeController.deleteProductType.error:{}", e.getMessage(), e);
            return Result.fail("删除产品分类失败！");
        }
    }

    /**
     * 获取产品分类列表
     */
    @Operation(summary = "获取产品列表", description = "查询")
    @PostMapping("/list")
    public Result<PageResult<ProductDTO>> getProductTypes(@RequestBody ProductTypeDTO productTypeDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductTypeController.getProductTypes.productTypeDTO:{}", productTypeDTO);
            }
            return Result.ok(productTypeService.getAllProductTypes());
        } catch (Exception e) {
            log.error("ProductTypeController.getProductTypes.error:{}", e.getMessage(), e);
            return Result.fail("查询产品列表失败！");
        }
    }

}
